var MOCK = false;
var root_url = "/bookstoreOnline"
if (MOCK) {
    document.write('<script src="../js/mock.js" type="text/javascript" charset="utf-8"></script>');
    document.write('<script src="../testAPI/loginAPI.js" type="text/javascript" charset="utf-8"></script>');
}

//登录操作
function login() {
    let uname = $('#uname-ipt').val();
    let pwd = $('#pwd-ipt').val();
    $.post(root_url + "/login", {
        uname: uname,
        pwd: pwd
    }, function (res, status) {
        res = JSON.parse(res);
        if (res.code == 1) {
            alert("登陆成功!");
            if (res.role == "admin") {
                //TODO
                window.location = ".." + root_url + "/admin";
            } else {
                window.location = ".." + root_url + "/user";
            }
        } else {
            alert("登录失败")
        }
    })
}

//注册操作
function register() {
    let uname = $("#uname-iptr").val();
    let pwd = $("#pwd-iptr").val();
    let con_pwd = $("#conpwd-iptr").val();
    let email = $("#email-iptr").val();
    if (pwd != con_pwd) {
        alert("确认密码与密码需相同");
        return;
    }
    $.post(root_url + "/register", {
        uname: uname,
        pwd: pwd,
        email: email,
        cellphone: "",
        mobilephone: "",
        address: ""
    }, function (res, status) {
        res = JSON.parse(res);
        if (res.code == 0) {
            alert(res.msg);
            return;
        }
        alert(res.msg);
        window.location="../"+root_url;
    })
}


function change_op_warp(op) {
    let login_warp = document.getElementsByClassName("login-warp");
    let register_warp = document.getElementsByClassName("register-warp");
    if (op == "register") {
        login_warp[0].style.display = "none";
        register_warp[0].style.display = "block";
    } else if (op == "login") {
        login_warp[0].style.display = "block";
        register_warp[0].style.display = "none";
    }
}
